package com.bestpractice.map;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

class Person {
	int id;
	String name;

	Person(int id, String name) {
		this.id = id;
		this.name = name;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getId() {
		return id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public String toString() {
		return id + "\t" + name;
	}
}

class SortByNameID implements Comparator {

	@Override
	public int compare(Object obj1, Object obj2) {
		Person p1 = (Person) obj1;
		Person p2 = (Person) obj2;
		Integer I1=p1.getId();
		Integer I2=p2.getId();
		int i = (p1.getName()).compareTo(p2.getName());
		if (i == 0) {
			/*if (p1.getId() > p2.getId()) {
				return 1;
			} else {
				return -1;
			}*/
			return I1.compareTo(I2);
		}

		return i;
	}

}

public class TestPerson {
	public static void main(String[] args) {
		List list = new ArrayList<>();
		list.add(new Person(1, "azmit"));
		list.add(new Person(9, "deepak"));
		list.add(new Person(8, "ajay"));
		list.add(new Person(4, "deepak"));
		for (Object o : list) {
			System.out.println(o);
		}
		System.out.println("-----------");
		Collections.sort(list, new SortByNameID());
		for (Object o : list) {
			System.out.println(o);
		}
	}
}
